package com.lagou.edu.course.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.edu.course.api.dto.*;
import com.lagou.edu.course.api.param.CourseQueryParam;
import com.lagou.edu.course.remote.CourseService;
import com.lagou.edu.course.remote.TeacherService;
import com.lagou.edu.course.service.IActivityCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.PrinterURI;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private IActivityCourseService activityCourseService;


    /**
     * 获取选课列表
     * @param userId
     * @return
     */
    @GetMapping("/getAllCourses")
    public List<CourseDTO> getAllCourses(@RequestParam(required = false,name = "userId") Integer userId) {
        return  courseService.getAllCourses(userId);
    }

  
    /**
     * 获取已购课程信息
     * @param userId
     * @return
     */
    @GetMapping("/getPurchasedCourse")
    List<CourseDTO> getPurchasedCourse(@RequestParam("userId") Integer userId){
        return  courseService.getPurchasedCourse(userId);
    }

    /**
     * 保存或者更新课程
     * @param courseDTO
     * @return
     */
    @PostMapping(value = "/saveOrUpdate")
    boolean saveOrUpdate(@RequestBody CourseDTO courseDTO){
        return courseService.saveOrUpdate(courseDTO);
    }
    /**
     * 后台查询课程详情的接口
     * @param courseId
     * @return
     */
    @GetMapping(value = "/getById")
    CourseDTO getById(@RequestParam("courseId") Integer courseId){
        CourseDTO courseDTO = courseService.getById(courseId);
        TeacherDTO teacherDTO = teacherService.getByCourseId(courseId);
        ActivityCourseDTO activityCourseDTO = activityCourseService.getByCourseId(courseId);
        if (activityCourseDTO != null) {
            courseDTO.setActivityCourseDTO(activityCourseDTO);
            courseDTO.setActivityCourse(true);
        }

        courseDTO.setTeacherDTO(teacherDTO);
        return courseDTO;
    }

    /**
     * 后台查询课程列表的接口
     */
    @PostMapping("/getUserPages")
    public Page<CourseDTO> getQueryCourses(@RequestBody CourseQueryParam courseQueryParam) {
        return courseService.getQueryCourses(courseQueryParam);
    }

    /**
     * 上架
     * @param id
     * @return
     */
    @PostMapping("/onShelves/{id}")
    boolean onShelves(@PathVariable("id") Integer id){
        return courseService.onShelves(id);
    }

    /**
     * 上架
     * @param id
     * @return
     */
    @PostMapping("/offShelves/{id}")
    public boolean offShelves(@PathVariable("id") Integer id){
        return courseService.offShelves(id);
    }


    @GetMapping(value = "/changeState")
    public boolean changeState(@RequestParam("courseId") Integer courseId,
                        @RequestParam("status") Integer status) {
        if (status.equals(0)) {
            courseService.offShelves(courseId);
        } else {
            courseService.onShelves(courseId);
        }
        return true;
    }

    @PostMapping(value = "/saveOrUpdateCourse",consumes = "application/json")
    Boolean saveOrUpdateCourse(@RequestBody CourseDTO courseDTO) {
        return courseService.saveCourse(courseDTO);
    }

}
